package BackGround;

import java.awt.*;
import javax.swing.*;
import java.io.File;

import Element.*;
import Element.Geom.*;
import DistSys.*;

/**
 * Title:        iPAD
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      ABB Inc.
 * @author Fangxing Li
 * @version 1.0
 */

public class BgImage extends AbsElem{
  private Image image;
  private String sImageSource;

  public BgImage(){
      this.geom = new BgImageGeom();
      this.setData(AbsElem.DATA_LAYER, ProjectInfo.BG_IMAGE_LAYER ); //Layer is used here!
  }

  public BgImage( String sFilePath) {
      this.updateImageSource(sFilePath);
      this.geom = new BgImageGeom();
      this.setData(AbsElem.DATA_LAYER, ProjectInfo.BG_IMAGE_LAYER ); //Layer is used here!
  }

  public void initFromScreen( int x1, int y1, int x2, int y2){
      this.geom.setNode1FromScreen( x1, y1);
      this.geom.setNode2FromScreen( x2, y2);
  }

  public void initFromStored( int x1Stored, int y1Stored, int x2Stored, int y2Stored){
      this.geom.setNode1FromStored( x1Stored, y1Stored);
      this.geom.setNode2FromStored( x2Stored, y2Stored);
  }

  public AbsElemGeom getElemGeomObj ( ){ return geom; }

  public Image  getImage(){ return image; }

  public String getImageSource() { return sImageSource; }
  public void setImageSource(String s) { sImageSource = s; }

  public void updateImageSource( String sFilePath){
      this.sImageSource = sFilePath;
      File f = new File( sFilePath);
      if( f.exists() == false ){
          this.image = null;
          return;
      }
      try{
          this.image = Toolkit.getDefaultToolkit().getImage( sFilePath );
          MediaTracker mediaTracker = new MediaTracker( new JFrame() );
          mediaTracker.addImage(this.image,0);
          mediaTracker.waitForID(0);
      }catch (Exception e){
      }
  }

  //draw bg images
  public void draw(Graphics g ){
      String errMsg = "Error in Loading Image";
      int index = ProjectInfo.BG_IMAGE_LAYER + ProjectInfo.NUM_BACK_GND_LAYER;
      Layer lyr = (Layer) Sys.prjInfo.layerList.get( index );
      if( lyr.getMode() == Layer.OFF ) return;

      int w = geom.x2 - geom.x1;
      int h = geom.y2 - geom.y1;

      if( image == null ) {
          g.setColor( Color.gray );
          g.fillRect( geom.x1, geom.y1, w, h );
          g.setColor( Color.white);
          g.drawString( errMsg, geom.x1, geom.y1 + h/2);
          return;
      }

      if( lyr.getMode() == Layer.ON ){
          g.drawImage( image, geom.x1, geom.y1, w, h, null ); }
      else { //GRAY mode
          g.drawImage( image, geom.x1, geom.y1, w, h, Color.gray, null ); }

      //draw bounding rectangel if needed
      if( this.geom.isSelected ) {
          g.setColor( Color.yellow);
          g.drawRect( geom.frame.x-1, geom.frame.y-1,
                      geom.frame.width, geom.frame.height);
      }
  }

  public void draw(Graphics2D g, double dist, double theta){ }

  public void drawForMove(Graphics g ){
      this.drawFrame( g );
  }

  public void drawFrame(Graphics g){
      super.drawFrame( g );
  }

  public void drawHighLights(Graphics g){
      //do nothing
  }

  public void drawNodes(Graphics g){
      //draw nodes if selected so that users can easily drag the image
      int x1=geom.x1, y1=geom.y1, x2=geom.x2, y2=geom.y2;
      int d=SysConst.NODE_SIZE ;      // diameter of node
      Color clrOld = g.getColor();
      g.setColor(Color.black);
      g.fillRect(x1-d/2,y1-d/2,d,d);  //starting dot
      g.fillRect(x2-d/2,y2-d/2,d,d);  //ending dot
      g.setColor( clrOld);
  }

  public void drawText(Graphics g ){
      //do nothing
  }

  public Object clone(){
      BgImage created = new BgImage();
      created.image = this.image; // refere to same image
      created.sImageSource = this.sImageSource;
      this.geom.copyTo( created.geom );

      return created;
  }

  //not used; just to implement abstract methods in AbsElem
  public int getType() { return SysConst.TYPE_BG_IMAGE; }
  public boolean getDataExists(int index) { return false; }
  public boolean getResultExists(int index) { return false; }

}

class BgImageGeom extends AbsElemGeom {
  public BgImageGeom() {
      this.setName("Background Image");
  }

  public void calcFrame(){
      frame.x = Math.min(x1, x2 );
      frame.y = Math.min(y1, y2);
      frame.width =  Math.abs( x2 - x1 );
      frame.height = Math.abs( y2 - y1 );
  }
}